package com.controller;

import com.entry.*;
import com.server.*;
import com.util.RedisUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
public class OrderFormController {
    @Resource
    ShoppingService shoppingService;
    @Autowired
    RedisUtil redisUtil;
    @Resource
    ProductService productService;
    @Resource
    AddressService addressService;
    @Resource
    OrderFormService orderFormService;
    @Resource
    MemberService memberService;
    @Resource
    SellService sellService;

    //购物车结算到订单页面
    @RequestMapping(value = "/order/paycar",method = RequestMethod.GET)
    @ResponseBody
    public Result  getShoppingList(@Param("shopping_checked")int shopping_checked,HttpSession session){
        Result result = new Result();
        int member_id=Integer.parseInt(String.valueOf(redisUtil.get("member_id")));
        String member_address=(String) redisUtil.get("member_address");
        //判断缓存是否有用户的member_id
        if (redisUtil.get("member_id")==null){

            result.setSuccess(false);
            return result;
        }
        Address address=addressService.get(member_address);
        System.out.println(address);
        session.setAttribute("address",address);


        /*System.out.println("获取省份信息");
        List<Address> addressList = addressService.getRoot();
        addressList.forEach(System.out::println);
        System.out.println("获取addressId为{}的市级地址信息:addressId");*/
        /*List<Address> cityAddress = addressService.getList(null, 61011361011361011366addressId);
        System.out.println("获取cityAddressId为{}的区级地址信息:cityAddressId");
        List<Address> districtAddress = addressService.getList(null, cityAddressId);*/


        List<Shopping> shoppingListPay= shoppingService.getShoppingListPay(member_id);
        session.setAttribute("shoppingListPay",shoppingListPay);
        //购买商品数量
        int countShopping=shoppingListPay.size();
        session.setAttribute("countShopping",countShopping);

        redisUtil.set("shopping_checkedCar","1");
        //总金额
        int sum_price=0;
        for (int i = 0; i < shoppingListPay.size(); i++) {
            int count= shoppingListPay.get(i).getShopping_product_amount();
            int singleprice= Integer.parseInt(shoppingListPay.get(i).getProduct().getProduct_price());
            sum_price+=count*singleprice;
        }
        //实付金额+运费
        int true_price=sum_price+6;
        System.out.println(sum_price);
        session.setAttribute("sum_price",sum_price);
        session.setAttribute("true_price",true_price);

        //当前时间日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        Date d = new Date(System.currentTimeMillis());
        System.out.println("当前日期时间为：" + formatter.format(d));
        //生成订单号
        String oreder = "";
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMddHH");
        Date d1 = new Date(System.currentTimeMillis());
        System.out.println("去掉格式的当前日期时间为：" + formatter1.format(d1));
        String dingdan=formatter1.format(d1);
        //生成随机三位数
        Random r=new Random();
        //在1000-9999
        int rand=r.nextInt(900)+100;
        System.out.println("随机数为："+rand);
        dingdan = dingdan + rand;
        System.out.println("订单号+日期+时间+随机数：" + dingdan);
        String ordr_code=dingdan;
        /*int ordr_code= rand;
        ordr_code+=156238759;*/

        session.setAttribute("ordr_code",ordr_code);
        redisUtil.set("ordr_code", String.valueOf(ordr_code));
        Member member=memberService.getMemberById(member_id);

        //redies缓存购物车支付ordr_code
        String ordr_code_shop=ordr_code;
        redisUtil.set("ordr_code_shop",ordr_code_shop);
        //添加订单
        String addresso=member_address;
        //OrderForm orderForm=new OrderForm(ordr_code, member_address,member.getMember_name(), member.getMember_phone(), d, member.getMember_id(), 0, "", true_price, "鲜花");
        int countOrderForm=orderFormService.addOrderForm(ordr_code, member.getMember_address(),member.getMember_name(), member.getMember_phone(), d, member.getMember_id(), 3, "商品描述", true_price, "鲜花");
        //int countSell= sellService.addSell(shoppingListPay.size(), true_price, ordr_code, 1, "0", "0");
        for (int i = 0; i < shoppingListPay.size(); i++) {
            int counta= shoppingListPay.get(i).getShopping_product_amount();
            int singlepricea= Integer.parseInt(shoppingListPay.get(i).getProduct().getProduct_price());
            int sum_singleprice=counta * singlepricea;
            //int countSell= sellService.addSell(counta, singlepricea, ordr_code, shoppingListPay.get(i).getProduct_id(), shoppingListPay.get(i).getProduct().getProduct_name(), shoppingListPay.get(i).getProduct_image());
            String Product_name=shoppingListPay.get(i).getProduct().getProduct_name();
            String Product_image= shoppingListPay.get(i).getProduct().getProductimages_src();
            int countSell= sellService.addSell(counta, sum_singleprice,ordr_code,shoppingListPay.get(i).getProduct_id(),Product_name,Product_image);
            //int a=shoppingService.delCartProduct(shoppingListPay.get(i).getMember_id(), shoppingListPay.get(i).getProduct_id());
        }
        //将商品添加到订单详情表里

        result.setSuccess(true);
        return result;
    }


    //立即购买直接结算到订单页面
    @RequestMapping(value = "/order/payfirstcar",method = RequestMethod.GET)
    @ResponseBody
    public Result  getShopping(@Param("product_id")int product_id,@Param("shopping_product_amount") int shopping_product_amount,HttpSession session){
        Result result = new Result();
        String member_id=String.valueOf(redisUtil.get("member_id"));
        String member_address=(String) redisUtil.get("member_address");
        System.out.println("product_id="+product_id);
        System.out.println("shopping_product_amount="+shopping_product_amount);

        //根据redies缓存的member_id判断用户
        if (member_id!=null){
            int id= Integer.parseInt(member_id);
            System.out.println("id="+id);
            Shopping shopping=shoppingService.getCountById(id, product_id);

            int count0=shoppingService.addCartChecked(id,product_id,shopping_product_amount,3);
            //shoppingService.updateCartChecked(product_id, id, 3);


            List<Shopping> shoppingListPay= shoppingService.getShoppingListPayList(id, product_id);
            session.setAttribute("shoppingListPay",shoppingListPay);
            //购买商品数量
            int countShopping=shoppingListPay.size();
            session.setAttribute("countShopping",countShopping);
            //总金额
            int sum_price=0;
            for (int i = 0; i < shoppingListPay.size(); i++) {
                int count= shoppingListPay.get(i).getShopping_product_amount();
                int singleprice= Integer.parseInt(shoppingListPay.get(i).getProduct().getProduct_price());
                sum_price+=count*singleprice;
            }
            //实付金额+运费
            int true_price=sum_price+6;
            System.out.println(sum_price);
            session.setAttribute("sum_price",sum_price);
            session.setAttribute("true_price",true_price);

            //当前时间日期
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
            Date d = new Date(System.currentTimeMillis());
            System.out.println("当前日期时间为：" + formatter.format(d));
            //生成订单号
            String oreder = "";
            SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd");
            Date d1 = new Date(System.currentTimeMillis());
            System.out.println("去掉格式的当前日期时间为：" + formatter1.format(d1));
            String dingdan=formatter1.format(d1);
            //生成随机三位数
            Random r=new Random();
            //在1000-9999
            int rand=r.nextInt(900)+100;
            System.out.println("随机数为："+rand);
            dingdan = dingdan + rand;
            System.out.println("订单号+日期+时间+随机数：" + dingdan);
            String ordr_code=dingdan;

            session.setAttribute("ordr_code",ordr_code);
            redisUtil.set("ordr_code", String.valueOf(ordr_code));
            Member member=memberService.getMemberById(id);
            redisUtil.set("shopping_checkedCarS","3");

            //redies缓存立即购买支付ordr_code
            String ordr_code_liji=ordr_code;
            redisUtil.set("ordr_code_liji",ordr_code_liji);

            //添加订单
            String addresso=member_address;
            //OrderForm orderForm=new OrderForm(ordr_code, member_address,member.getMember_name(), member.getMember_phone(), d, member.getMember_id(), 0, "", true_price, "鲜花");
            int countOrderForm=orderFormService.addOrderForm(ordr_code, member.getMember_address(),member.getMember_name(), member.getMember_phone(), d, member.getMember_id(), 3, "商品描述", true_price, "鲜花");
            //int countSell= sellService.addSell(shoppingListPay.size(), true_price, ordr_code, 1, "0", "0");
            for (int i = 0; i < shoppingListPay.size(); i++) {
                int counta= shoppingListPay.get(i).getShopping_product_amount();
                int singlepricea= Integer.parseInt(shoppingListPay.get(i).getProduct().getProduct_price());
                int sum_singleprice=counta * singlepricea;
                //int countSell= sellService.addSell(counta, singlepricea, ordr_code, shoppingListPay.get(i).getProduct_id(), shoppingListPay.get(i).getProduct().getProduct_name(), shoppingListPay.get(i).getProduct_image());
                String Product_name=shoppingListPay.get(i).getProduct().getProduct_name();
                String Product_image= shoppingListPay.get(i).getProduct().getProductimages_src();
                int countSell= sellService.addSell(counta, sum_singleprice,ordr_code,shoppingListPay.get(i).getProduct_id(),Product_name,Product_image);
                //int a=shoppingService.delCartProductByChecked(shoppingListPay.get(i).getMember_id(), shoppingListPay.get(i).getProduct_id());
            }



            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }



    //生成订单支付
    @RequestMapping("/order/paymoney")
    public String payinput(HttpSession session){

        String ordr_code=(String) redisUtil.get("ordr_code");
        OrderForm orderForm= orderFormService.getOrderFormByOrderCode(ordr_code);

        session.setAttribute("orderForm",orderForm);

        return "pay/payDemo";
    }

    //自动加载获取订单列表
    @RequestMapping(value = "/OrderForm/getOrderForm",method = RequestMethod.GET)
    @ResponseBody
    public Result getOrderForm(HttpSession session) {

        Result result = new Result();
        String member_id=String.valueOf(redisUtil.get("member_id"));
        if (member_id!=null){
            List<OrderForm> orderFormList=orderFormService.getOrderSell(Integer.parseInt(member_id));
            session.setAttribute("orderFormList",orderFormList);
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }
}
